<template>
  <!-- 头部 -->
  <header class="header">
    <!-- 头部的第一行 -->
    <div class="top">
      <div class="container">
        <div class="loginList">
          <p>贝贝商城欢迎您！</p>
          <p v-if="!userInfo.name">
            <span>请</span>
            <router-link to="/login">登录</router-link>
            <router-link to="/register" class="register">免费注册</router-link>
          </p>
          <p v-else>
            <a>{{ userInfo.name }}</a>
            <a class="register" @click="exitLogin" href="javascript:void(0)">退出登陆</a>
          </p>
        </div>
        <div class="typeList">
          <router-link to="/center/myOrder">我的订单</router-link>
          <router-link to="/shopCart">我的购物车</router-link>
          <a href="###">我的贝贝商城</a>
          <a href="###">贝贝商城会员</a>
          <a href="###">企业采购</a>
          <a href="###">关注贝贝商城</a>
          <a href="###">合作招商</a>
          <a href="###">商家后台</a>
        </div>
      </div>
    </div>
    <!--头部第二行 搜索区域-->
    <div class="bottom">
      <h1 class="logoArea">
        <router-link class="logo" to="/home">
          <img src="./images/logo.jpg" alt="">
        </router-link>
      </h1>
      <div class="searchArea">
        <form action="###" class="searchForm">
          <input
              type="text"
              id="autocomplete"
              class="input-error input-xxlarge"
              v-model="keyword"
              @keydown.enter="goSearch"
          />
          <button class="sui-btn btn-xlarge btn-danger"
                  type="button"
                  @click="goSearch"
          >搜索
          </button>
        </form>
      </div>
    </div>
  </header>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "MyHeader",
  methods: {
    goSearch() {
      this.$router.push({
        //常见面试题
        //1.使用了params，则必须用name选项，而不能用path选项
        //2.如何指定params参数是否能传入？如果不穿入params，但已经在route中配置了占位符，路径会出现问题。指定其能否传递，在站位后加上'?'，即表示可有可无
        //3.params如果传递的是空串，如何解决？如果传入空串，则会路径出错；使用undefined来解决
        //4.路由组件能否传递props？可以；且有3种写法
        name: 'search',
        params: {
          keyword: this.keyword || undefined
        },
        query: this.$route.query
      })
    },
    async exitLogin() {
      //1.给服务器发送请求，清除token
      //2.清除本地token
      try {
        await this.$store.dispatch('user/exitLogin')
        //3.退回首页
        await this.$router.push('/home')
      } catch (error) {
        alert(error.message)
      }
    },
    async getUserInfo() {
      try {
        await this.$store.dispatch('user/getUserInfo')
      } catch (error) {
        //未登陆成功，说明token失效
        await this.exitLogin()
      }
      this.$bus.$on('clearKeyword', () => {
        this.keyword = ''
      })
    }
  },
  data() {
    return {
      keyword: ''
    }
  },
  mounted() {
    this.getUserInfo() //不要给mounted加上async
  },
  beforeDestroy() {
    this.$bus.$off('clearKeyword')
  },
  computed: {
    ...mapState('user', ['userInfo'])
  }
}
</script>

<style scoped lang="less">
.header {
  & > .top {
    background-color: #eaeaea;
    height: 30px;
    line-height: 30px;

    .container {
      width: 1200px;
      margin: 0 auto;
      overflow: hidden;

      .loginList {
        float: left;

        p {
          float: left;
          margin-right: 10px;

          .register {
            border-left: 1px solid #b3aeae;
            padding: 0 5px;
            margin-left: 5px;
          }
        }
      }

      .typeList {
        float: right;

        a {
          padding: 0 10px;

          & + a {
            border-left: 1px solid #b3aeae;
          }
        }

      }

    }
  }

  & > .bottom {
    width: 1200px;
    margin: 0 auto;
    overflow: hidden;

    .logoArea {
      float: left;

      .logo {
        img {
          width: 175px;
          margin: 25px 45px;
        }
      }
    }

    .searchArea {
      float: right;
      margin-top: 35px;

      .searchForm {
        overflow: hidden;

        input {
          box-sizing: border-box;
          width: 490px;
          height: 32px;
          padding: 0px 4px;
          border: 2px solid #ea4a36;
          float: left;

          &:focus {
            outline: none;
          }
        }

        button {
          height: 32px;
          width: 68px;
          background-color: #ea4a36;
          border: none;
          color: #fff;
          float: left;
          cursor: pointer;

          &:focus {
            outline: none;
          }
        }
      }
    }
  }
}
</style>